PBDS Set
์ ๊ดํ ๊ฐ๋จํ ์ ๋ฆฌ. ์๊ฒ ๋์๋๋ฐ ์ ๋ฆฌ ์ํ๋ฉด ๋์ค์ ๋ค์ ๋งจ๋
์์ ๊ตฌ๊ธ๋ง ํด์ผํ๋ฏ๋ก ํ๋ฒ ์๊ฒ ๋ ๋ด์ฉ ์ ๋ฆฌํ๊ณ ๊ฐ๋ณด๋ ค๊ณ ํ๋ค.
PBDS๋ Policy based data structures์ ์ฝ์๋ก, g++
์์ ext/pb_ds
์๋์ ์์นํ๋ ๋ฐ์ดํฐ ๊ตฌ์กฐ๋ฅผ ์นญํ๋ค. (MSVC์์๋ ์ฌ์ฉ์ด ์ด๋ ค์ธ ๋ฏ) ๋ณดํต PS
์์ ์ด๊ฒ์ ์ฌ์ฉํ๋ ์์ ์ค ํ๋๋ ๊ธฐ์กด STL Set
์์ ์ง์ํ์ง ์๋ K
๋ฒ์งธ ํฌ๊ธฐ๊ฐ ํฐ(๋๋ ์์) ์์๋ฅผ ๋น ๋ฅด๊ฒ ์ฐพ์ ์ ์๊ฒ ํ๋ ๋ฐ์ดํฐ ๊ตฌ์กฐ๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด ์ฌ์ฉํ๋ ๊ฒ์ด๋ค.
์ผ๋ฐ set
์ ์ฌ์ฉํ๋ ๋๋์ผ๋ก ์ฌ์ฉํ ์ ์๊ฒ define
ํด์ ์ฌ์ฉํ๋ค. ๊ธ ํ๋จ์ ์์๊ฐ ์๋ค. ์ฌ๊ธฐ์๋ ordered_set
์ผ๋ก ์ฌ์ฉํ๋ค.
์ผ๋ฐ set
์์ ์ฌ์ฉ๋๋ method
๋ฅผ ์ฌ์ฉํจ๊ณผ ๋๋ถ์ด(์ฌ์ง์ด iterator
๋ ์ฌ์ฉ๊ฐ๋ฅํจ์ ํ์ธํ๋ค) ์ถ๊ฐ๋ก ์ฌ์ฉ๋๋ method
๋ find_by_order(k)
์ order_of_key(key)
์ด๋ค. ์ด๋ฆ ๊ทธ๋๋ก, k
๋ฒ์งธ ์์๋ฅผ ์ ์ฐพ์ ์ ์๊ฒ ํ๊ฑฐ๋, key
๊ฐ์ ์ฃผ์์ ๋ ๊ทธ๊ฒ์ด ๋ช๋ฒ์งธ ์์์ธ์ง๋ฅผ ์ ์๊ฒ ํด์ค๋ค. ์ผ๋ฐ set
์์ ๊ฐ์ ์ผ์ ํ ์ ์์ผ๋ ค๋ฉด ์ ์๊ฐ๋ณต์ก๋์๋ ๊ฒ์ ๊ฐ์ํ๋ฉด ํฐ ์ฐจ์ด์ด๋ค.
ordered_set X;X.insert(1);X.insert(2);X.insert(4);X.insert(8);X.insert(16);cout<<*X.find_by_order(1)<<endl; // 2cout<<*X.find_by_order(2)<<endl; // 4cout<<*X.find_by_order(4)<<endl; // 16cout<<(end(X)==X.find_by_order(6))<<endl; // truecout<<X.order_of_key(-5)<<endl; // 0cout<<X.order_of_key(1)<<endl; // 0cout<<X.order_of_key(3)<<endl; // 2cout<<X.order_of_key(4)<<endl; // 2cout<<X.order_of_key(400)<<endl; // 5
BOJ 3653: https://www.acmicpc.net/problem/3653
์ ์ธ๋ถ
#include <ext/pb_ds/assoc_container.hpp>#include <ext/pb_ds/tree_policy.hpp>using namespace __gnu_pbds;struct Node {int priority; // ๋์์๋ก ์์ชฝ์int id;bool operator<(const Node& t) const {if (priority != t.priority) return priority < t.priority;return id < t.id;}bool operator>(const Node& t) const {return t < *this;}};#define ordered_set tree<Node, null_type, less<Node>, rb_tree_tag,tree_order_statistics_node_update>
์ฌ๊ธฐ์ ์ฌ์ฉํ ๊ตฌํ์ฒด๋ rb_tree
์ด๋ค. ๊ทธ๋ฐ๋ฐ, ์ข ์ฐพ์๋ณด๋ ์ด ๊ตฌํ์ฒด๋ฅผ splay_tree
๋ฑ์ผ๋ก๋ ๋ฐ๊ฟ ์ ์๋๊ฑฐ ๊ฐ๋ค. ์๊ฐ ์ ํ์ด ๋นก๋นกํ๋ค๋ฉด ์ด๋ฐ ๊ตฌํ์ฒด๋ฅผ ๋ฐ๊ฟ๊ฐ๋ฉด์ ํ
์คํธํด๋ด๋ ์ข์ ๋ฏ.
void solve() {ordered_set S;unordered_map<int, ordered_set::point_iterator> M;int N, CASE; scanf("%d %d", &N, &CASE);for(int i = 1; i <= N; ++i) {auto res = S.insert({N - i + 1, i});auto it = res.first;M.emplace(i, it);}// for(auto& it: S) {// printf("(%d, %d)\n", it.priority, it.id);// }int priority = N + 1;for(int i = 0; i < CASE; ++i) {int c; scanf("%d", &c);auto mit = M.find(c);auto sit = mit->second;M.erase(mit);// ์๋ ๋ถ๋ถ์ด ๋ช ๋ฒ์งธ ์์์ธ์ง ๋น ๋ฅด๊ฒ ์ฐพ๋ ๋ถ๋ถ์ด๋ค. ์ผ๋ฐ set์๋ ์๋ methodint cdist = S.order_of_key(*sit); //distance(sit, S.end());int cid = sit->id;S.erase(sit);auto res = S.insert({priority, cid});auto it = res.first;priority++;M.emplace(cid, it);printf("%d ", N - cdist - 1);}printf("\n");}
์ด ๋ฌธ์ ์ ๊ฒฝ์ฐ ๋ฑ ์ฝ๊ธฐ๋ง ํด๋ Ordered Set์ด ์ฌ์ฉ๋๋ฉด ํธํ๊ฒ ํ ์ ์๊ฒ ๋ค๋ ์๊ฐ์ด ๋ ๋ค. ์ฌ๋ผ์ด๋ฉ ์๋์ฐ ๊ธฐ๋ฒ์ผ๋ก ์์๋ฅผ ๋ฃ๊ณ ๋นผ๋ค๊ฐ, (K - 1) / 2 ๋ฒ์งธ ์์๋ฅผ ๋น ๋ฅด๊ฒ ์ฐพ์ ์ ์๊ธฐ๋ง ํ๋ฉด ์ฝ๊ฒ ๊ตฌํ์ด ๋๋ ๋ฌธ์ ์ด๋ค.
#include <bits/stdc++.h>using namespace std;#include <ext/pb_ds/assoc_container.hpp>#include <ext/pb_ds/tree_policy.hpp>using namespace __gnu_pbds;struct Node {int degree;int id;bool operator<(const Node& t) const {if (degree != t.degree) return degree < t.degree;return id < t.id;}bool operator>(const Node& t) const {return t < *this;}};#define ordered_set tree<Node, null_type, less<Node>, rb_tree_tag,tree_order_statistics_node_update>ordered_set v;
id์ ๊ฒฝ์ฐ ์ค๋ณต๊ฐ ์ฒ๋ฆฌ๋ฅผ ์ํด ์กด์ฌํ๋ค. (์ค๋ณต๋์ ์ฌ๋ผ์ง๋ ๊ฒ์ ๋ฐฉ์ง)
int A[250000];int main() {int N, K; scanf("%d %d", &N, &K);for(int i = 0; i < N; ++i) scanf("%d", &A[i]);int cur = 0;for(int i = 0; i < K; ++i) {v.insert({A[i], i});}int mid = (K - 1) / 2;long long ans = v.find_by_order(mid)->degree;int l = 0;int r = K;for(; r < N; ++r, ++l) {v.insert({A[r], r});v.erase(v.find({A[l], l}));ans += v.find_by_order(mid)->degree;}printf("%lld\n", ans);return 0;}